//
//  HMEmoticonModel.swift
//  表情键盘
//
//  Created by WGP on 16/4/20.
//  Copyright © 2016年 WGP. All rights reserved.
//

import UIKit

class HMEmoticonModel: NSObject, NSCoding {
    
    var id: String
    
//    emoji表情使用
    var code: String?
    {
        didSet
        {
            let scanner = NSScanner(string: code!)
            var result: UInt32 = 0
            
            scanner.scanHexInt(&result)
            
            let scalar = UnicodeScalar(result)
            let c = Character(scalar)
            
            emoji = String(c)
        }
    }
    
    var emoji: String?
    
    ///中文名称
    var chs: String?
    
    ///图片名称
    var png: String?
    {
        didSet
        {
//            图片的全路径 = mainBundle/id/png
            fullPngPath = emoticonsBundlePath + "/" + id + "/" + png!
        }
    }
    
    var fullPngPath: String?
    
    init(dict: [String: String], id: String) {
        self.id = id
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description:String
    {
        get
        {
            return "\n\t\t:表情模型: chs:\(chs), png: \(png), code:\(code)"
        }
    }
    
//    归档和解档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(chs, forKey: "chs")
        aCoder.encodeObject(code, forKey: "code")
        aCoder.encodeObject(png, forKey: "png")
        aCoder.encodeObject(emoji, forKey: "emoji")
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        self.id = aDecoder.decodeObjectForKey("id") as? String ?? ""
        self.chs = aDecoder.decodeObjectForKey("chs") as? String
        self.code = aDecoder.decodeObjectForKey("code") as? String
        self.png = aDecoder.decodeObjectForKey("png") as? String
        self.emoji = aDecoder.decodeObjectForKey("emoji") as? String
        super.init()
        
        if self.png != nil
        {
            self.fullPngPath = emoticonsBundlePath + "/" + id + "/" + png!
        }
    }
}
    








